package com.example.bgmachine.bgmachine.martirelpro;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.warters.WaterRippleView;

import java.util.ArrayList;

public class HomepageMertrielProgressAdapter extends BaseAdapter {

    private Context context;


    private ArrayList<HomepageMertrielProgressItemData> list = new ArrayList<HomepageMertrielProgressItemData>();

    public HomepageMertrielProgressAdapter(Context context, ArrayList<HomepageMertrielProgressItemData> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public HomepageMertrielProgressItemData getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        HomepageMertrielProgressAdapter.ViewHolder holder = null;
        if (convertView == null) {
            View view=View.inflate(context, R.layout.homepage_mertriel_progress_layout, null);
            convertView = view;
            holder  = new HomepageMertrielProgressAdapter.ViewHolder();
            holder.hmepageMertrielProgressItemData = list.get(position);
            holder.mertriel_progress_item_text = view.findViewById(R.id.mertriel_progress_item_text);
            holder. mWaterRippleView= view.findViewById(R.id.mertriel_water_ripple_view);
            holder.mertriel_progress_item_pb = view.findViewById(R.id.mertriel_progress_item_pb);
            holder.mertriel_progress_item_pb.setEnabled(false);
            holder.mertriel_progress_item_text.setText(holder.hmepageMertrielProgressItemData.getMenuName());

            holder.mertriel_progress_item_pb.setMax(holder.hmepageMertrielProgressItemData.getFullMmaterial());
            int values = holder.hmepageMertrielProgressItemData.getFullMmaterial()-holder.hmepageMertrielProgressItemData.getUseMmaterial();
            holder.mertriel_progress_item_pb.setProgress(values);


            float maxs = (float)holder.hmepageMertrielProgressItemData.getFullMmaterial();
            float valuea = (float)values;
            float waveLevelRatio = (float)valuea /  maxs;
            holder.mWaterRippleView.setWaveLevelRatio(waveLevelRatio);
            if(holder.hmepageMertrielProgressItemData.getFullMmaterial() <= 0){
           //     holder.mertriel_progress_item_pb.setVisibility(RelativeLayout.GONE);
                holder.mWaterRippleView.setVisibility(RelativeLayout.GONE);
            }else {
            //    holder.mertriel_progress_item_pb.setVisibility(RelativeLayout.VISIBLE);
                holder.mWaterRippleView.setVisibility(RelativeLayout.VISIBLE);
                if(values <= 20 *holder.hmepageMertrielProgressItemData.getFullMmaterial()/ 100 ){
                    // 加载自定义的drawable资源
                    Drawable customDrawable = convertView.getResources().getDrawable(R.drawable.homepage_progressbar_lack_backgound_style);

                    // 设置进度条的drawable样式
                    holder.mertriel_progress_item_pb.setProgressDrawable(customDrawable);
                 //   holder. mWaterRippleView.setWaveColor(R.color.progress_lack_background,R.color.progress_lack_background);
                    holder. mWaterRippleView.setWaveColor(0x33C5080A,0x99C5080A);

                }
                if(values > 20 *holder.hmepageMertrielProgressItemData.getFullMmaterial()/ 100 && values <= 60 *holder.hmepageMertrielProgressItemData.getFullMmaterial()/ 100 ){
                    // 加载自定义的drawable资源
                    Drawable customDrawable = convertView.getResources().getDrawable(R.drawable.homepage_progressbar_half_backgound_style);

                    // 设置进度条的drawable样式
                    holder.mertriel_progress_item_pb.setProgressDrawable(customDrawable);
               //     holder. mWaterRippleView.setWaveColor(R.color.progress_half_background,R.color.progress_half_background);
                    holder. mWaterRippleView.setWaveColor(0x33FFBF4A,0x99FFBF4A);

                }
                if( values > 60 *holder.hmepageMertrielProgressItemData.getFullMmaterial()/ 100 ){

                    // 加载自定义的drawable资源
                    Drawable customDrawable = convertView.getResources().getDrawable(R.drawable.homepage_progressbar_backgound_style);

                    // 设置进度条的drawable样式
                    holder.mertriel_progress_item_pb.setProgressDrawable(customDrawable);
                 //   holder. mWaterRippleView.setWaveColor(R.color.progress_background,R.color.progress_background);
                    holder. mWaterRippleView.setWaveColor(0x3375BA28,0x9975BA28);
                }
            }

           // mertriel_progress_item_pb.setProgressDrawable();
            convertView.setTag(holder);

        }else{
            holder = (HomepageMertrielProgressAdapter.ViewHolder) convertView.getTag();
            holder.hmepageMertrielProgressItemData = list.get(position);
            holder.mertriel_progress_item_text = convertView.findViewById(R.id.mertriel_progress_item_text);
            holder. mWaterRippleView= convertView.findViewById(R.id.mertriel_water_ripple_view);
            holder.mertriel_progress_item_pb = convertView.findViewById(R.id.mertriel_progress_item_pb);
            holder.mertriel_progress_item_pb.setEnabled(false);
            holder.mertriel_progress_item_text.setText(holder.hmepageMertrielProgressItemData.getMenuName());


            holder.mertriel_progress_item_pb.setMax(holder.hmepageMertrielProgressItemData.getFullMmaterial());
            int values = holder.hmepageMertrielProgressItemData.getFullMmaterial()-holder.hmepageMertrielProgressItemData.getUseMmaterial();
            holder.mertriel_progress_item_pb.setProgress(values);

            float maxs = (float)holder.hmepageMertrielProgressItemData.getFullMmaterial();

            float valuea = (float)values;
            float waveLevelRatio = (float)valuea /  maxs;
            holder.mWaterRippleView.setWaveLevelRatio(waveLevelRatio);

            if(holder.hmepageMertrielProgressItemData.getFullMmaterial() <= 0){
             //  holder.mertriel_progress_item_pb.setVisibility(RelativeLayout.GONE);
                holder.mWaterRippleView.setVisibility(RelativeLayout.GONE);

            }else {
             //   holder.mertriel_progress_item_pb.setVisibility(RelativeLayout.VISIBLE);
                holder.mWaterRippleView.setVisibility(RelativeLayout.VISIBLE);
                if(values <= 20 *holder.hmepageMertrielProgressItemData.getFullMmaterial() / 100 ){
                    // 加载自定义的drawable资源
                    Drawable customDrawable = convertView.getResources().getDrawable(R.drawable.homepage_progressbar_lack_backgound_style);

                    // 设置进度条的drawable样式
                    holder.mertriel_progress_item_pb.setProgressDrawable(customDrawable);
                    holder. mWaterRippleView.setWaveColor(0x33C5080A,0x99C5080A);
                }
                if(values > 20 *holder.hmepageMertrielProgressItemData.getFullMmaterial()/ 100 && values <= 60 *holder.hmepageMertrielProgressItemData.getFullMmaterial()/ 100 ){
                    // 加载自定义的drawable资源
                    Drawable customDrawable = convertView.getResources().getDrawable(R.drawable.homepage_progressbar_half_backgound_style);

                    // 设置进度条的drawable样式
                    holder.mertriel_progress_item_pb.setProgressDrawable(customDrawable);
                 //   holder. mWaterRippleView.setWaveColor(R.color.progress_half_background,R.color.progress_half_background);
                    holder. mWaterRippleView.setWaveColor(0x33FFBF4A,0x99FFBF4A);

                }
                if( values > 60 *holder.hmepageMertrielProgressItemData.getFullMmaterial()/ 100 ){

                    // 加载自定义的drawable资源
                    Drawable customDrawable = convertView.getResources().getDrawable(R.drawable.homepage_progressbar_backgound_style);

                    // 设置进度条的drawable样式
                    holder.mertriel_progress_item_pb.setProgressDrawable(customDrawable);
               //    holder. mWaterRippleView.setWaveColor(R.color.progress_background,R.color.progress_background);
                    holder. mWaterRippleView.setWaveColor(0x3375BA28,0x9975BA28);

                }
            }

        }
        return convertView;
    }

    public class ViewHolder {
        TextView mertriel_progress_item_text = null;
        ProgressBar mertriel_progress_item_pb = null;
        HomepageMertrielProgressItemData hmepageMertrielProgressItemData = null;
        WaterRippleView mWaterRippleView = null;
    }

}
